package csc.daonjpa.java.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import share.Constant;
import csc.daonjpa.java.domain.Customer;

@Repository
public class CustomerDAO {

	@PersistenceContext
	EntityManager entityManager;
	
	public Customer getCustomerByCustomerId(Long customerId) {

		Customer customer = null;
		// -----------Begin transaction-----------
		try {
			// Get a list of accounts from DB
			customer = entityManager.find(Customer.class, customerId);
		} catch (Exception e) {
			entityManager.close();
		}

		return customer;

	}
	
	public Customer checkLogin(String id, String pass) {

		String md5CodePass = Constant.getMD5Code(pass);
		
		Customer customer = null;
		try {
			TypedQuery<Customer> query = entityManager
					.createQuery(
							"SELECT c FROM "
									+ Customer.class.getName()
									+ " c Where c.loginId= :loginId and c.password= :password",
							Customer.class);
			query.setParameter("loginId", id);
			query.setParameter("password", md5CodePass);
			
			customer = query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customer;
	}
	
	@Transactional
	public boolean updatePassWord(Long customerId, String oldPass, String newPass){
		boolean check = false;
		String md5OldPass = Constant.getMD5Code(oldPass);
		String md5NewPass = Constant.getMD5Code(newPass);
		try{
			Customer customer = entityManager.find(Customer.class, customerId);
			
			if(md5OldPass.equals(customer.getPassword())){
				customer.setPassword(md5NewPass);
				check = true;
			}
			else{
				check = false;
			}
			
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
		
		return check;
	}
	
	public Customer getCustomerByAccountNumber(String accountNumber) {
		Customer cus = null;
		try{
			TypedQuery<Customer> query = entityManager
					.createQuery(
							"SELECT c FROM "
									+ Customer.class.getName()
									+ " c Where c.accountNumber= :accountNumber",
							Customer.class);
			query.setParameter("accountNumber", accountNumber);
			cus = query.getSingleResult();
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
		return cus;
	}
	
	@Transactional
	public void resetFailedCount(Long customerId){
		try {
			Customer customer = entityManager.find(Customer.class, customerId);
			customer.setFailedCount(0);			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Customer getCustomerByLoginId(String loginId){
		Customer cus = null;
		try {
			TypedQuery<Customer> query = entityManager
					.createQuery(
							"SELECT c FROM "
									+ Customer.class.getName()
									+ " c Where c.loginId= :loginId",
									Customer.class);
			query.setParameter("loginId", loginId);
			cus = query.getSingleResult();			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cus;
	}
	
	@Transactional
	public void updateFailedCount(Customer customer){
		int failedCount;
		try{
			failedCount = customer.getFailedCount();
			customer.setFailedCount(failedCount+1);
			entityManager.merge(customer);			
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
	}
}
